﻿/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Site_payment接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Site_paymentRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Site_paymentRepository : BaseRepository<Site_payment, Int32>, ISite_paymentRepository
    {
        public Site_paymentRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption = options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public List<Payment> GetList(string strWhere)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select ");
                strSql.Append(" S.*,P.img_url,P.title as ptitle,P.remark,P.type,P.poundage_type,P.poundage_amount,P.redirect_url,P.return_url,P.notify_url,P.is_lock");
                strSql.Append(" FROM [" + nameof(Payment) + "] as P INNER JOIN [" + nameof(Site_payment) + "] as S ON P.id=S.payment_id");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
                strSql.Append(" order by S.sort_id asc,S.id desc");
                return _dbConnection.Query<Payment>(strSql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 获取支付平台实体
        /// </summary>
        public Models.Payment GetPaymentModel(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select top 1 P.* FROM [" + nameof(Payment) + "] as P INNER JOIN [" + nameof(Site_payment) + "] as S ON P.id=S.payment_id");
                strSql.Append(" and S.id=" + id);
                return _dbConnection.Query<Payment>(strSql.ToString()).FirstOrDefault();
            }
        }

        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Site_payment) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
    }
}